home *** CD-ROM | disk | FTP | other *** search
- Path: news.gate.net!!bcfreenet!z007400b
- From: z007400b@bcfreenet.seflin.lib.fl.us (Ralph Silverman)
- Newsgroups: comp.lang.c
- Subject: Re: Binary files ?
- Date: 7 Mar 1996 19:40:51 GMT
- Organization: SEFLIN Free-Net - Broward
- Message-ID: <4hne43$ihi@news.seflin.lib.fl.us>
- References: <4gheee$bvr@ci.ist.utl.pt> <Pine.SV4.3.91-heb-2.04.960224055809.17461A-100000@cs.technion.ac.il>
- NNTP-Posting-Host: ns2.seflin.lib.fl.us
- X-Newsreader: TIN [version 1.2 PL2]
-
- Kohn Emil Dan (emild@cs.technion.ac.il) wrote:
-
-
- : On 22 Feb 1996, delphis wrote:
-
- : >
- : >
- : >
- : >
- : > Greetings from a frustrated c programmer!
- : > I program in c a couple of years but i never figure out this basic
- : > question : how to work with binary files ? basically, what i want to do
- : > is writing a matrix to a file and read it back but when i try the
- : > following code in dos the matrix comes all gobled, but works fine in unix!
- : > Can anyone tell me whats wrong with this ? I have been using text files
- : > and they dont give any troble at all!
- : >
- : > Obrigadissimo!
- : >
- : >
- : >
- : >
- : > # include <stdio.h>
- : >
- : >
- : > void main()
- : > {
- : >
- : > /*write file*/
- : > int n;
- : > unsigned char mat[200];
- : > FILE *f=fopen("teste","w"); /*do i have to use the CREATE before this ?*/
- : > for (n=0;n<200;n++) mat[n]=n; /*init matrix*/
- : > fwrite(mat,200,1,f);
- : > fclose(f);
- : >
- : > /*reset matrix*/
- : > for (n=0;n<200;n++) mat[n]=0;
- : >
- : > /*read file*/
- : > f=fopen("teste","r");
- : > fread(mat,200,1,f);
- : > fclose(f);
- : > for (n=0;n<200;n++) printf("%d ",mat[n]);/*print it*/
- : > }
- : >
- : >
-
- : Just open the file using "rb" for reading and "wb" for writing, (instead
- : of "r", and "w" respectively, and everything will be ok.
-
- : In UNIX, there is no difference between text and binary files, a file is
- : a stream of bytes. In DOS, the situation is much stranger, I think he
- : expands \n to \n\r, and puts an EOF (CTRL-Z) at the end of file, when a
- : file is opened in text mode.
-
- : And last, but not least: main() returns int, not void...
-
-
- : Hope this helps.
-
- : Regards,
-
- : Emil
-
- --
- *********begin r.s. response*************
-
-
- prior response is accurate...
- the answer is to go to
- low level i.o.
- ^^^^^^^^^^^^^^
- .
-
- *********end r.s. response***************
- Ralph Silverman
- z007400b@bcfreenet.seflin.lib.fl.us
-
-